package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Transactional
@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;
    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐的基本信息
        this.save(setmealDto);
       Long id = setmealDto.getId();
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        System.out.println(setmealDishes);
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(id);
        }
         setmealDishService.saveBatch(setmealDishes);
    }

//    @Override
//    public void removeWithDish(List<Long> ids) {
//        //查询套餐状态，确定是否能够删除
//        LambdaQueryWrapper<Setmeal> wrapper= new LambdaQueryWrapper<>();
//        wrapper.in(Setmeal::getId,ids);
//        wrapper.eq(Setmeal::getStatus,1);
//        int count = this.count(wrapper);
//        if(count>0){
//            throw new CustomException("套餐正在售卖中，不能删除");
//        }
//        //如果可以删除，先删除套餐表中的数据
//        this.removeByIds(ids);
//        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
//        lqw.in(SetmealDish::getId,ids);
//        setmealDishService.remove(lqw);
//    }
    /**
     * 删除套餐，同时需要删除套餐和菜品的关联数据
     * @param ids
     */
  @Override
    public void removeWithDish(List<Long> ids) {
        //select count(*) from setmeal where id in (1,2,3) and status = 1
        //查询套餐状态，确定是否可用删除
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.in(Setmeal::getId,ids);
        queryWrapper.eq(Setmeal::getStatus,1);

        int count = this.count(queryWrapper);
        if(count > 0){
            //如果不能删除，抛出一个业务异常
            throw new CustomException("套餐正在售卖中，不能删除");
        }

        //如果可以删除，先删除套餐表中的数据---setmeal
        this.removeByIds(ids);
        //delete from setmeal_dish where setmeal_id in (1,2,3)
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);
        //删除关系表中的数据----setmeal_dish
        setmealDishService.remove(lambdaQueryWrapper);
    }

    @Override
    public SetmealDto getByIdWithDish(Long id) {
        //根据id查询Setmeal表中的基本信息
        Setmeal setmeal = this.getById(id);
         //创造一个setmealdto对象
        SetmealDto setmealDto = new SetmealDto();
        //拷贝对象
        BeanUtils.copyProperties(setmeal,setmealDto);
        //查询关联表setmeal_dish的菜品信息
        LambdaQueryWrapper<SetmealDish> lqw= new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(lqw);
        setmealDto.setSetmealDishes(list);
        return  setmealDto;

    }
    @Override
    public void updatewithDish(SetmealDto setmealDto) {
//        //更新dish表基本信息
//       this.updateById(setmealDto);
//        Long setmealid = setmealDto.getId();
//        log.info("setmealid的id为：{}",setmealid);
//        //清理当前菜品对应口味数据---dish_flavor表中的delete操作
//        LambdaQueryWrapper<SetmealDish> lqw= new LambdaQueryWrapper<>();
//        lqw.eq(SetmealDish::getSetmealId,setmealid);
//        setmealDishService.remove(lqw);
//        //添加当前提交过来的口味数据--dish_flavor表中的insert操作
//        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
//        for (SetmealDish setmealDish : setmealDishes) {
//            setmealDish.setSetmealId(setmealid);
//        }
//        setmealDishService.saveBatch(setmealDishes);
        //更新dish表基本信息
        this.updateById(setmealDto);
        Long setmealId = setmealDto.getId();
        log.info("setmealid的id为：{}",setmealId);
        //清理当前套餐对应的菜品-- setmeal_dish表中
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,setmealId);
        boolean remove = setmealDishService.remove(lqw);
        //添加当前提交过来的菜品信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealId);
        }
        setmealDishService.saveBatch(setmealDishes);



    }

}
